#include "CAnimationManager.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "CAnimation.h"
#include <fstream>
#include <iostream>

CAnimationManager::CAnimationManager(void)
{
	Animate = new CAnimation;
	NumberofAnimes = 0;
}
CAnimationManager::~CAnimationManager(void)
{
	delete Animate;
}

CAnimationManager* CAnimationManager::GetInstance()
{
	static CAnimationManager Instance;

	return &Instance;
}
void CAnimationManager::Init()
{

}
void CAnimationManager::Clear()
{

}
void CAnimationManager::Shutdown()
{

}
void CAnimationManager::Load(const char* szFilePath)
{	
	ifstream fin(szFilePath, std::ios_base::binary);
	fin.exceptions(~std::ios_base::goodbit);
	try
	{
		unsigned int byteStrLen = 0;
		fin.read(((char*)&NumAnim), sizeof(int));
		for(int i = 0; i < NumAnim; ++i)
		{		
			//Animation Name
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(&m_szAniName,0,byteStrLen);
			fin.read(((char*)&m_szAniName), byteStrLen);
			fin.clear();
			m_szAniName[byteStrLen] = 0;
			char buffer[256];
			sprintf_s(buffer, _countof(buffer), m_szAniName);

			Animate->SetAniName(m_szAniName);

			//Number of Frames
			fin.read((char*)&m_nTotalFrames, sizeof(int));

			Animate->SetTotalFrames(m_nTotalFrames);

			//Load in File Path
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(&m_szImageFile,0,byteStrLen);
			fin.read(((char*)&m_szImageFile), byteStrLen);
			fin.clear();
			m_szImageFile[byteStrLen] = 0;
			sprintf_s(buffer, _countof(buffer), "Resource/Animations/%s", m_szImageFile);

			//is it looping
			fin.read((char*)&m_bIsLooping, sizeof(bool));

			Animate->SetIsLooping(m_bIsLooping);

			//Trigger Name
			fin.read((char*)&byteStrLen, sizeof(char));
			fin.clear();
			memset(&m_szTrigName,0,byteStrLen);
			fin.read(((char*)&m_szTrigName), byteStrLen);
			fin.clear();
			m_szTrigName[byteStrLen] = 0;
			sprintf_s(buffer, _countof(buffer), "Resource/Animations/%s", m_szTrigName);

			Animate->SetTrigName(m_szTrigName);

			
			for(int j = 0; j < m_nTotalFrames; ++j)
			{
				fin.read((char*)&m_nFrameCount, sizeof(int));
				fin.read((char*)&m_fFrameLength, sizeof(int));
				fin.read((char*)&m_rRenderRect.left, sizeof(int));
				fin.read((char*)&m_rRenderRect.top, sizeof(int));
				fin.read((char*)&m_rRenderRect.right, sizeof(int));
				fin.read((char*)&m_rRenderRect.bottom, sizeof(int));
				fin.read((char*)&m_rAttackRect.left, sizeof(int));
				fin.read((char*)&m_rAttackRect.top, sizeof(int));
				fin.read((char*)&m_rAttackRect.right, sizeof(int));
				fin.read((char*)&m_rAttackRect.bottom, sizeof(int));
				fin.read((char*)&m_rCollisionRect.left, sizeof(int));
				fin.read((char*)&m_rCollisionRect.top, sizeof(int));
				fin.read((char*)&m_rCollisionRect.right, sizeof(int));
				fin.read((char*)&m_rCollisionRect.bottom, sizeof(int));
				fin.read((char*)&m_nAnchorPointX, sizeof(int));
				fin.read((char*)&m_nAnchorPointY, sizeof(int));
				fin.read((char*)&m_nWeaponPointX, sizeof(int));
				fin.read((char*)&m_nWeaponPointY, sizeof(int));

				m_rRenderRect.right  = m_rRenderRect.left + m_rRenderRect.right;
				m_rRenderRect.bottom = m_rRenderRect.top + m_rRenderRect.bottom;
				m_rAttackRect.right	= m_rAttackRect.left + m_rAttackRect.right;
				m_rAttackRect.bottom = m_rAttackRect.top + m_rAttackRect.bottom;
				m_rCollisionRect.right = m_rCollisionRect.left + m_rCollisionRect.right;
				m_rCollisionRect.bottom	= m_rCollisionRect.top + m_rCollisionRect.bottom;

				Frame.SetFrameLength(m_fFrameLength);
				Frame.SetRenderRect(m_rRenderRect);
				Frame.SetAttackRect(m_rAttackRect);
				Frame.SetCollisionRect(m_rCollisionRect);
				Frame.SetAnchorPointX(m_nAnchorPointX);
				Frame.SetAnchorPointY(m_nAnchorPointY);
				Frame.SetWeaponPointX(m_nWeaponPointX);
				Frame.SetWeaponPointY(m_nWeaponPointY);
				Frame.SetFrameCount(m_nFrameCount);

				Animate->AddFrame(Frame, NumberofAnimes);
			}
			
			char temp[256];
			sprintf_s(temp, _countof(temp), "Resource/graphics/%s", m_szImageFile);
			Animate->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture(temp, D3DCOLOR_XRGB(255, 255, 255)));
			AddAnimation(*Animate);
			NumberofAnimes++;
		}
		fin.close();
	}
	catch(std::ios_base::failure)
	{
		fin.close();
		MessageBox(NULL,
			"File Read Error","Error",MB_OK);
	}

}
void CAnimationManager::Update(float fDelta, Animations* Anim)
{
	Animate->Update(fDelta, Anim);
}
void CAnimationManager::Render(int nPosX, int nPosY, float fScale, DWORD dwColor, Animations Anim, bool bIsFlipped)
{
	m_vAnimations[Anim.nCurrAnimation].Render(nPosX, nPosY, fScale, dwColor, &Anim, bIsFlipped);
}
void CAnimationManager::AddAnimation(CAnimation pAnimation)
{
	m_vAnimations.push_back(pAnimation);
}